package com.ruoyi.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.DTO.TCourseInfoDTO;
import com.ruoyi.student.domain.TSpeciality;
import com.ruoyi.student.domain.vo.TCourseInfoVo;
import com.ruoyi.student.service.ITSpecialityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.TCourseInfo;
import com.ruoyi.student.service.ITCourseInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/student/course")
public class TCourseInfoController extends BaseController
{
    @Autowired
    private ITCourseInfoService tCourseInfoService;
    @Autowired
    private ITSpecialityService itSpecialityService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('student:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCourseInfo tCourseInfo)
    {
        startPage();
//        List<TCourseInfo> list = tCourseInfoService.selectTCourseInfoList(tCourseInfo);
        List<TCourseInfoVo> list = tCourseInfoService.selectTCourseInfoVoList(tCourseInfo);
        return getDataTable(list);
    }

    /**
     * 查询未添加的课程列表
     */
    @GetMapping("/listNoCourse")
    public TableDataInfo listNoCourse(TCourseInfoDTO tCourseInfoDTO)
    {
        System.out.println(tCourseInfoDTO);
        startPage();
        List<TCourseInfoVo> list = tCourseInfoService.selectNoCourseTCourseInfoVoList(tCourseInfoDTO);
        return getDataTable(list);
    }



    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('student:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCourseInfo tCourseInfo)
    {
        List<TCourseInfo> list = tCourseInfoService.selectTCourseInfoList(tCourseInfo);
        ExcelUtil<TCourseInfo> util = new ExcelUtil<TCourseInfo>(TCourseInfo.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
//        return success(tCourseInfoService.selectTCourseInfoById(id));
        return success(tCourseInfoService.selectTCourseInfoVoById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('student:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCourseInfo tCourseInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tCourseInfo.setCreatorId(user.getUserId());
        TSpeciality tSpeciality = itSpecialityService.selectTSpecialityById(Long.valueOf(tCourseInfo.getSpecialityId()));
        tCourseInfo.setSpecialityName(tSpeciality.getSpecialityName());
        return toAjax(tCourseInfoService.insertTCourseInfo(tCourseInfo));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('student:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCourseInfo tCourseInfo)
    {
        return toAjax(tCourseInfoService.updateTCourseInfo(tCourseInfo));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('student:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCourseInfoService.deleteTCourseInfoByIds(ids));
    }
}
